<template>
  <div>
    <el-dialog title="公司信息" :visible.sync="show" width="50%" append-to-body>
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        inline
        v-show="showSearch"
        label-width="100px"
      >
        <el-form-item label="公司名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入公司名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </el-row>
      <el-table
        v-loading="loading"
        :data="companyList"
        border
        stripe
        ref="companyTable"
        :header-cell-class-name="cellClass"
        @select="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" type="index" width="55" align="center">
          <template slot-scope="scope">
            <span>{{
              (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="公司名称" align="center" prop="name" />
        <el-table-column label="负责人姓名" align="center" prop="userName" />
        <el-table-column label="负责人电话" align="center" prop="phoneNumber" />
        <el-table-column label="公司地址" align="center" prop="address" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompany } from "@/api/weiriya/company";

export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    // 是否单选
    single: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false, // 遮罩层
      chooseList: [], // 选中数组
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      companyList: [], // 公司信息表格数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        userName: null,
      },
    };
  },
  computed: {
    show: {
      get() {
        return this.value;
      },
      set() {
        this.$emit("input", false);
      },
    },
  },
  watch: {
    value(val) {
      if (val) this.getList();
    },
  },
  methods: {
    // 查询公司信息列表
    getList() {
      this.loading = true;
      listCompany(this.queryParams)
        .then((res) => {
          this.companyList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 禁止全选和多选
    cellClass(row) {
      if (this.single && row.columnIndex == 0) {
        return "DisableSelection";
      }
    },
    // 多选框选中数据
    handleSelectionChange(val, row) {
      if (this.single) {
        if (this.chooseList.some((i) => i.id == row.id)) {
          this.$refs.companyTable.toggleRowSelection(row, false);
          this.chooseList = [];
        } else {
          this.$refs.companyTable.clearSelection();
          this.$refs.companyTable.toggleRowSelection(row, true);
          this.chooseList = [row];
        }
      } else {
        this.chooseList = val;
      }
    },
    // 提交按钮
    submitForm() {
      this.$emit("comfirm", this.single ? this.chooseList[0] : this.chooseList);
      this.show = false;
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-table .DisableSelection .cell .el-checkbox__inner {
  display: none;
}
</style>
